// NjtUserController.java (放置在ruoyi-admin模块中)
package com.ruoyi.web.controller.nongjt.user;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.nongjt.domain.pojo.NjtOrganization;
import com.ruoyi.nongjt.domain.pojo.NjtUser;
import com.ruoyi.nongjt.service.NjtOrganizationService;
import com.ruoyi.nongjt.service.NjtUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/nongjt/user")
public class NjtUserController extends BaseController {

    @Autowired
    private NjtUserService userService;

    @Autowired
    private NjtOrganizationService organizationService;

    /**
     * 查询普通用户列表
     */
//    @PreAuthorize("@ss.hasPermi('nongjt:user:list')")
    @GetMapping("/list")
    public TableDataInfo list(NjtUser user) {
        startPage();
        List<NjtUser> list = userService.selectUserList(user);
        return getDataTable(list);
    }

    /**
     * 查询单位列表
     */
//    @PreAuthorize("@ss.hasPermi('nongjt:user:list')")
    @GetMapping("/organizations")
    public AjaxResult organizations() {
        List<NjtOrganization> list = organizationService.selectOrganizationList(new NjtOrganization());
        return AjaxResult.success(list);
    }

    /**
     * 获取未完善信息用户列表
     */
//    @PreAuthorize("@ss.hasPermi('nongjt:user:list')")
    @GetMapping("/incomplete")
    public TableDataInfo incompleteUsers() {
        startPage();
        NjtUser user = new NjtUser();
        user.setPerfectFlag("0"); // 未完善信息的用户
        List<NjtUser> list = userService.selectUserList(user);
        return getDataTable(list);
    }

    /**
     * 查询用户详细信息
     */
//    @PreAuthorize("@ss.hasPermi('nongjt:user:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(userService.selectUserById(id));
    }

    /**
     * 新增普通用户
     */
//    @PreAuthorize("@ss.hasPermi('nongjt:user:add')")
//    @Log(title = "普通用户管理", businessType = BusinessType.INSERT)
//    @PostMapping
//    public AjaxResult add(@RequestBody NjtUser user) {
//        if (user.getMobilePhone() == null || user.getMobilePhone().isEmpty()) {
//            return AjaxResult.error("请输入手机号码");
//        }
//
//        // 检查手机号是否已存在
//        NjtUser existingUser = userService.selectUserByMobilePhone(user.getMobilePhone());
//        if (existingUser != null) {
//            return AjaxResult.error("手机号码已存在");
//        }
//
//        return toAjax(userService.insertUser(user));
//    }
    /**
     * 新增普通用户
     */
    @Log(title = "普通用户管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody NjtUser user) {
        if (user.getMobilePhone() == null || user.getMobilePhone().isEmpty()) {
            return AjaxResult.error("请输入手机号码");
        }

        // 检查手机号是否已存在
        NjtUser existingUser = userService.selectUserByMobilePhone(user.getMobilePhone());
        if (existingUser != null) {
            return AjaxResult.error("手机号码已存在");
        }

        // 自动生成注册时间
        user.setRegisterTime(new Date());

        return toAjax(userService.insertUser(user));
    }


    /**
     * 修改普通用户
     */
//    @PreAuthorize("@ss.hasPermi('nongjt:user:edit')")
    @Log(title = "普通用户管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody NjtUser user) {
        if (user.getId() == null || user.getId() == 0) {
            return AjaxResult.error("用户ID不能为空");
        }
        return toAjax(userService.updateUser(user));
    }

    /**
     * 删除普通用户
     */
//    @PreAuthorize("@ss.hasPermi('nongjt:user:remove')")
    @Log(title = "普通用户管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        for (Long id : ids) {
            userService.deleteUserById(id);
        }
        return success();
    }
}
